use ${supp_data}/taxsim/pwages_only_returned, clear

rename v14 exemption
rename v22 ctc
rename v24 ccare
rename v25 eitc
rename v28 fbctax
*duplicates tag year pwages mstat dep18 depx, gen(dup)
sort year pwages mstat dep18 depx v30

bysort year pwages mstat dep18 depx: gen n = _n
keep if n==1
drop n


order year pwages mstat dep18 depx  fiitax eitc exemp ccare ctc


preserve 
keep year pwages mstat dep18 depx  fiitax fbctax eitc exemp ccare ctc
foreach x of varlist dep18 depx  fiitax fbctax eitc exemp ccare ctc {
	rename `x' nc_`x'
}
gen dep18 = nc_dep18-1 
gen depx = nc_depx-1 
save temp, replace
restore

merge 1:1 year pwages mstat dep18 depx using temp , gen(_m_nc) keep(match master)

foreach x in fiitax fbctax eitc exemp ccare ctc {
gen d_`x' = nc_`x'-`x'
}
replace d_fiitax = -d_fiitax
replace d_fbctax = -d_fbctax

replace d_exemp = (frate/100)*d_exemp
replace d_exemp = 0 if frate<0

merge m:1 year using "${supp_data}/cpi_u", gen(_m_cpi) keep(match master)

gen exempval = exemption*(frate/100)
replace exempval = 0 if frate<=0

foreach x in pwages d_eitc d_fiitax d_fbctax d_exemp eitc exemption exempval {
	capture gen `x'_adj = (cpi_2015/jan)*`x'
}

gen p_d = d_fiitax_adj/pwages_adj
gen p = (eitc_adj+exempval_adj)/pwages_adj


gen k = 0 if dep18==0 & depx==0
replace k = 1 if dep18==1 & depx==1


*Setup Merge
gen bo_cap = 1 if dep18==0 & depx==0
replace bo_cap = 2 if dep18==1 & depx==1
replace bo_cap = 3 if dep18==2 & depx==2
replace bo_cap = 4 if dep18==3 & depx==3

preserve 
gen long agiz_taxsim_unadj = pwages
gen agiz_taxsim = pwages_adj
gen deitc_taxsim = d_eitc_adj
gen dtax_taxsim = d_fiitax_adj


keep if bo_cap!=.
keep year bo_cap mstat agiz_taxsim* deitc_taxsim dtax_taxsim eitc nc_eitc frate *fiitax *fbctax dep* 
order year bo_cap mstat agiz_taxsim* deitc_taxsim dtax_taxsim eitc nc_eitc frate *fiitax *fbctax dep* 

save ${supp_data}/dtax_taxsim_slim, replace
restore

